package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.ConfigurationManager;
import com.innovation.library.util.InnovationAlgorithm;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteEditText;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;

public class PasswordActivity extends BaseActivity implements CuteToolbar.OnClickCuteToolbarListener {

    private CuteEditText mEditOldPassword;
    private CuteEditText mEditNewPassword;
    private CuteEditText mEditNewPassword2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        CuteToolbar toolbar = (CuteToolbar) findViewById(R.id.toolbar);
        toolbar.setOnClickCuteToolbarListener(this);
        mEditOldPassword = (CuteEditText) findViewById(R.id.edit_old_password);
        mEditNewPassword = (CuteEditText) findViewById(R.id.edit_new_password);
        mEditNewPassword2 = (CuteEditText) findViewById(R.id.edit_new_password2);
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

        LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return;
        }
        String phone = loginInfo.getPhone();
        String token = loginInfo.getToken().getAccessToken();
        String oldP = mEditOldPassword.getText().toString();
        String newP = mEditNewPassword.getText().toString();
        String newP2 = mEditNewPassword2.getText().toString();

        if (TextUtils.isEmpty(oldP)) {
            tipShow("请先提供旧密码!");
            return;
        }

        if (TextUtils.isEmpty(newP)) {
            tipShow("请输入新密码");
            return;
        }

        if (TextUtils.isEmpty(newP2)) {
            tipShow("请确认新密码");
            return;
        }

        if (!newP.equals(newP2)) {
            tipShow("两次新密码输入不一致，请重新输入!");
            return;
        } else {
            // 正则表达式,6~16,不能包含空格.
            if (newP2.length() < 6 || newP2.length() > 16 || newP2.trim().lastIndexOf(" ") != -1) {
                tipShow("新密码格式不正确，要求6-16位字符，不能包含空格!");
                return;
            }
        }


        oldP = InnovationAlgorithm.SHA1(Config.getConfig(ConfigurationManager.PASSWORD_SALT), oldP);
        newP2 = InnovationAlgorithm.SHA1(Config.getConfig(ConfigurationManager.PASSWORD_SALT), newP2);

        Client.requestPasswordUpdate(this, phone, token, oldP, newP2, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {

                tipShow("密码修改成功，请重新登录!");

                dismissProgressDialog();

                Intent intentExit = new Intent();
                intentExit.setAction("cleanLogin");
                sendBroadcast(intentExit);

                Config.cleanLogin(PasswordActivity.this);
                Intent intent = new Intent(PasswordActivity.this, LoginActivity.class);
                startActivity(intent);

                finish();
            }

            @Override
            public void onChZhRJFailure(String value) {
                if (value.equals("用户不存在或者密码错误")) {
                    tipShow("旧密码输入不正确，请重新输入!");
                }
            }

            @Override
            public void onInnovationFinish() {
                dismissProgressDialog();
            }

            @Override
            public void onInnovationExceptionFinish() {
                dismissProgressDialog();
            }

        }, new Client.NetworkUnavailableListener() {
            @Override
            public void networkUnavailable() {
                tipShow("网络不稳定，请稍后再试!");
                dismissProgressDialog();
            }
        });
    }
}
